package com.sensePlusVR.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

/**
 * 
 * @author: Loren
 * @create: 2016-7-1上午10:33:18
 * @desc: Toast消息封装
 *
 */
@SuppressWarnings("unused")
public class MyToast {
	private static Handler handler = new Handler(Looper.getMainLooper());
	private static Toast toast = null;
	private static final int SHOW_TIME = 5*1000;
	
	private final static int DEFAULT = 1000;
	private static int duration = 0;
	private static int currDuration = 0;
	
	
    private static Runnable mToastThread = new Runnable() {
    public void run() {
    	toast.show();
    	handler.postDelayed(mToastThread, DEFAULT);// 每隔2秒显示一次
        if (duration != 0) {
            if (currDuration <= duration) {
                currDuration += DEFAULT;
            } 
            else {
                cancel();
            }
        }
        
        }
    };
    
	/**
	 * Toast发送String消息，默认Toast.LENGTH_SHORT,多个消息不覆盖
	 * @param act
	 * @param msg
	 */
	public static void showMessage(Context act,String msg){
		//showMessage(act, msg, SHOW_TIME);
		showToast(act, msg);
	}
	

	
	/**
	 * Toast发送int消息，默认Toast.LENGTH_SHORT,多个消息不覆盖
	 * @param act
	 * @param msg
	 */
	public static void showMessage(Context act,int msg){
		//showMessage(act, msg, SHOW_TIME);
		showToast(act, msg);
	}
	

    private static void showToast(Context mContext, String msg) {
        if (toast == null) {
            toast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
        }
        else {
            toast.setText(msg);
        }
        toast.show();
    }
    
    private static void showToast(Context mContext, int id) {
        if (toast == null) {
            toast = Toast.makeText(mContext, id, Toast.LENGTH_LONG);
        }
        else {
            toast.setText(id);
        }
        toast.show();
    }
	


    public static void showMessage(Context context,String msg,int len){
    	showMessageTime(context, msg, len);
    }
    
    public static void showMessage(Context context,int msg,int len){
    	showMessageTime(context, msg, len);
    }
    
    public static void showMessageTime(Context context,int msg,int len){
         currDuration=DEFAULT;
         if(toast == null){
        	 toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
         }
         setText(msg);
         show(len);
    }
    
    public static void showMessageTime(Context context,String msg,int len){
        currDuration=DEFAULT;
        if(toast == null){
        	toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        }
        setText(msg);
        show(len);
   }
    
    public static void setText(String text) {
    	toast.setText(text);
    }
    
    public static void setText(int text) {
    	toast.setText(text);
    }
    
    public static void setView(View view){
        toast.setView(view);
    }
    
    private static void show(int len) {
        duration = len;
        cancelCurrent();
        handler.post(mToastThread);
    }
    
    private static void cancelCurrent(){
    	handler.removeCallbacks(mToastThread);// 先把显示线程删除
        currDuration = DEFAULT;
    }
    
    private static void cancel() {
    	handler.removeCallbacks(mToastThread);// 先把显示线程删除
        toast.cancel();// 把最后一个线程的显示效果cancel掉，就一了百了了
        currDuration = DEFAULT;
    }
}
